package me.project.jobster.controller;

import me.project.jobster.common.constant.GlobalConstants;
import me.project.jobster.common.util.Response;
import me.project.jobster.dao.mybatis.model.Company;
import me.project.jobster.dao.mybatis.model.Student;
import me.project.jobster.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * user login and register
 *
 * @author Mcdull
 * @date 2018-4-26
 */
@Controller
@RequestMapping("user")
public class LoginController extends BaseController{
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LoginService loginService;


    /**
     * register page init
     *
     * @param model
     * @return
     */
    @GetMapping("register/index")
    public String registerIndex(Model model) {
        return "login/register";
    }

    /**
     * login page init
     *
     * @param model
     * @return
     */
    @GetMapping("login/index")
    public String loginIndex(Model model) {
        return "login/login";
    }

    @PostMapping("register")
    public String register(HttpServletRequest request, Model model) {
        //To distinguish student or company (0:student,1:company)
        String flag = request.getParameter("flag");
        String email = request.getParameter("user_email");
        String name = request.getParameter("user_name");
        String password = request.getParameter("password");
        if (GlobalConstants.STUDENT.equals(flag)) {
            Response response = loginService.studentRegister(name, email, password);
            if (response.isSuccess()) {
                logger.info("save a student!");
                model.addAttribute("successMsg", "Register Success!");
                return "login/login";
            } else {
                model.addAttribute("errorMsg", response.getRespMessage());
                return "login/register";
            }
        } else {
            Response response = loginService.companyRegister(name, email, password);
            if (response.isSuccess()) {
                logger.info("save a company!");
                model.addAttribute("successMsg", "Register Success!");
                return "login/login";
            } else {
                model.addAttribute("errorMsg", response.getRespMessage());
                return "login/register";
            }
        }
    }

    /**
     * login page
     *
     * @param flag
     * @param email
     * @param password
     * @param request
     * @param model
     * @return
     */
    @PostMapping("login")
    public String login(@RequestParam("flag") String flag,
                        @RequestParam("email") String email,
                        @RequestParam("password") String password,
                        HttpServletRequest request, Model model) {
        // if params are empty
        if (StringUtils.isEmpty(email)) {
            logger.info("Email is empty");
            model.addAttribute("errorMsg", "Email is empty");
            return "login/login";
        } else if (StringUtils.isEmpty(password)) {
            logger.info("Password is empty");
            model.addAttribute("errorMsg", "Password is empty");
            return "login/login";
        }

        HttpSession session = request.getSession();
        // 0: student, 1:company
        session.setAttribute("user_flag", flag);
        if (GlobalConstants.STUDENT.equals(flag)) {
            logger.info("student:{} ready to login.", email);
            Response response = loginService.studentLogin(email, password);
            if (!response.isSuccess()) {
                model.addAttribute("errorMsg", response.getRespMessage());
                return "login/login";
            }
            Student student = (Student) response.getResult();
            //userId
            session.setAttribute("user_id", student.getsId());
            session.setAttribute("user_name", student.getName());
            model.addAttribute("student", student);

            session.setAttribute(GlobalConstants.MENU, "00");
            countMsgUnread(request);
            return "student/profile";
        } else {
            logger.info("company:{} ready to login.", email);
            Response response = loginService.companyLogin(email, password);
            if (!response.isSuccess()) {
                model.addAttribute("errorMsg", response.getRespMessage());
                return "login/login";
            }
            Company company = (Company) response.getResult();
            //UserId
            session.setAttribute("user_id", company.getcId());
            session.setAttribute("user_name", company.getName());
            model.addAttribute("company", company);

            session.setAttribute(GlobalConstants.MENU, "10");
            countMsgUnread(request);
            return "company/profile";
        }
    }

    @GetMapping("logout")
    public String logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.invalidate();
        return "login/login";
    }


}
